home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 4287 / 4287.xpi / chrome / splitbrowser.jar / content / splitbrowser / animationManager.js next >
Text File  |  2009-11-05  |  3KB  |  135 lines

  1. /*
  2.  Animation Task Manager
  3.  
  4.  Usage:
  5.    window['piro.sakura.ne.jp'].animationManager.addTask(
  6.      function(aTime, aBeginningValue, aTotalChange, aDuration) {
  7.        // some animation task runned by interval
  8.        var current = someEasingFunction(aTime, aBeginningValue, aTotalChange, aDuration);
  9.        target.style.left = current+'px';
  10.        return aTime > aDuration; // return true if the animation finished.
  11.      },
  12.      100, // beginning
  13.      200, // total change (so, the final value will be 100+200=300)
  14.      250  // msec, duration
  15.    );
  16.    // stop all
  17.    window['piro.sakura.ne.jp'].animationManager.stop();
  18.    // restart after doing something
  19.    window['piro.sakura.ne.jp'].animationManager.start();
  20.  
  21.  lisence: The MIT License, Copyright (c) 2009 SHIMODA "Piro" Hiroshi
  22.    http://www.cozmixng.org/repos/piro/fx3-compatibility-lib/trunk/license.txt
  23.  original:
  24.    http://www.cozmixng.org/repos/piro/fx3-compatibility-lib/trunk/animationManager.js
  25. */
  26. (function() {
  27.     const currentRevision = 4;
  28.  
  29.     if (!('piro.sakura.ne.jp' in window)) window['piro.sakura.ne.jp'] = {};
  30.  
  31.     var loadedRevision = 'animationManager' in window['piro.sakura.ne.jp'] ?
  32.             window['piro.sakura.ne.jp'].animationManager.revision :
  33.             0 ;
  34.     var tasks = !loadedRevision ? [] : window['piro.sakura.ne.jp'].animationManager.tasks ;
  35.     if (loadedRevision && loadedRevision > currentRevision) {
  36.         return;
  37.     }
  38.  
  39.     var Cc = Components.classes;
  40.     var Ci = Components.interfaces;
  41.  
  42.     if (tasks.length)
  43.         window['piro.sakura.ne.jp'].animationManager.stop();
  44.  
  45.     window['piro.sakura.ne.jp'].animationManager = {
  46.         revision : currentRevision,
  47.  
  48.         addTask : function(aTask, aBeginningValue, aTotalChange, aDuration) 
  49.         {
  50.             if (!aTask) return;
  51.             this.tasks.push({
  52.                 task      : aTask,
  53.                 start     : (new Date()).getTime(),
  54.                 beginning : aBeginningValue,
  55.                 change    : aTotalChange,
  56.                 duration  : aDuration
  57.             });
  58.             if (this.tasks.length == 1)
  59.                 this.start();
  60.         },
  61.  
  62.         removeTask : function(aTask) 
  63.         {
  64.             if (!aTask) return;
  65.             var task;
  66.             for (var i in this.tasks)
  67.             {
  68.                 task = this.tasks[i];
  69.                 if (task.task != aTask) continue;
  70.                 delete task.task;
  71.                 delete task.start;
  72.                 delete task.beginning;
  73.                 delete task.change;
  74.                 delete task.duration;
  75.                 this.tasks.splice(i, 1);
  76.                 break;
  77.             }
  78.             if (!this.tasks.length)
  79.                 this.stop();
  80.         },
  81.  
  82.         start : function()
  83.         {
  84.             this.stop();
  85.             this.timer = window.setInterval(
  86.                 this.onAnimation,
  87.                 this.interval,
  88.                 this
  89.             );
  90.         },
  91.  
  92.         stop : function() 
  93.         {
  94.             if (!this.timer) return;
  95.             window.clearInterval(this.timer);
  96.             this.timer = null;
  97.         },
  98.  
  99.         removeAllTasks : function()
  100.         {
  101.             this.stop();
  102.             this.tasks = [];
  103.         },
  104.  
  105.         tasks    : tasks,
  106.         interval : 10,
  107.         timer    : null,
  108.  
  109.         onAnimation : function(aSelf) 
  110.         {
  111.             // task should return true if it finishes.
  112.             var now = (new Date()).getTime();
  113.             aSelf.tasks = aSelf.tasks.filter(function(aTask) {
  114.                 try {
  115.                     return !aTask.task(
  116.                         now - aTask.start,
  117.                         aTask.beginning,
  118.                         aTask.change,
  119.                         aTask.duration
  120.                     );
  121.                 }
  122.                 catch(e) {
  123.                 }
  124.                 return false;
  125.             });
  126.             if (!aSelf.tasks.length)
  127.                 aSelf.stop();
  128.         }
  129.  
  130.     };
  131.  
  132.     if (tasks.length)
  133.         window['piro.sakura.ne.jp'].animationManager.start();
  134. })();
  135.